﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRM.DataObjects;
using Telerik.Web.UI;
using System.Data;
using SRM;

public partial class ApplicantFormView : System.Web.UI.Page, IApplicantForm
{
    public DataTable Applicant { get; set; }
    public string ApplicantKey { get; set; }

    public string PreviousApplicant { get; set; }
    public string NextApplicant { get; set; }


    protected void Page_Load(object sender, EventArgs e)
    {

        //if ((!Request.IsAuthenticated) || (Session["ApplicantKey"] == null))
        //{
        //    if (Request.QueryString.Count > 0)
        //        Response.Redirect("~/LoginForm.aspx?" + Request.QueryString.ToString());
        //    else
        //        Response.Redirect("~/LoginForm.aspx");
        //}
        if ((Session["ApplicantKey"] == null) && (Request.QueryString["key"] != null))
        {
            Session["ApplicantKey"] = Request.QueryString["key"].Trim();
        }


        this.hdn_UseSAPCode.Value = "1";
        this.ApplicantKey = Session["ApplicantKey"].ToString();
        this.hdn_applicantKey.Value = Session["ApplicantKey"].ToString();
        int applicantKey = int.Parse(Session["ApplicantKey"].ToString());

        #region Setup ApplicantKey

        if (Request.QueryString["key"] != null)
        {
            if (!int.TryParse(Request.QueryString["key"], out applicantKey))
            {
                applicantKey = int.Parse(Session["ApplicantKey"].ToString());
            }
            else
            {
                Session["ApplicantKey"] = applicantKey;
            }
        }

        #endregion Setup ApplicantKey

        DataTable dtPrevNext = SRM_Applicant.GetPrevAndNextApplicant(applicantKey.ToString());
        if (dtPrevNext.Rows.Count > 0)
        {
            if (dtPrevNext.Rows[0]["ApplicantKey"] == DBNull.Value)
                PreviousApplicant = string.Empty;
            else
                PreviousApplicant = string.Format("<a href=\"ApplicantForm.aspx?key={0}\">prev</a>", dtPrevNext.Rows[0]["ApplicantKey"].ToString());

            if (dtPrevNext.Rows[1]["ApplicantKey"] == DBNull.Value)
                NextApplicant = string.Empty;
            else
                NextApplicant = string.Format("<a href=\"ApplicantForm.aspx?key={0}\">next</a>", dtPrevNext.Rows[1]["ApplicantKey"].ToString());
        }


        #region Setup Position (if any else unsolicited)

        if ((Session["RequisitionKey"] == null) && (Request.QueryString["pos"] == null))
        {
            Session["RequisitionKey"] = SRM_Requisition.GetUnsolicitedRequisitionKey();
        }
        else
        {
            string requisitionKey = string.Empty;

            if ((string.IsNullOrEmpty(requisitionKey)) && (Session["RequisitionKey"] != null))
                requisitionKey = Session["RequisitionKey"].ToString().Trim();

            if ((string.IsNullOrEmpty(requisitionKey)) && (Request.QueryString["pos"] != null))
                requisitionKey = Request.QueryString["pos"].Trim();

            Session["RequisitionKey"] = requisitionKey.ToString();
        }

        #endregion Setup Position (if any else unsolicited)

        this.Applicant = SRM_Applicant.GetApplicant(applicantKey);
    }

}